//
//  NeighbouringViewController.m
//  HeartTravel
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "NeighbouringViewController.h"
#import "NeighbouringCell.h"
#import "HTTPTool.h"
#import "NeighbouringModel.h"
#import "NeighbouringDetailCell.h"
#import "NeighbouringDetailTableCell.h"
#import "MJRefresh.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface NeighbouringViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain)NSArray *titleArr;

@property(nonatomic, retain)UITableView *tableView;

@property(nonatomic, assign)NSInteger isSelected;

//@property(nonatomic, copy)NSString *test;

@property(nonatomic, retain)NSMutableArray *allArr;

@property(nonatomic, retain)NSMutableArray *scenicArr;

@property(nonatomic, retain)NSMutableArray *stayArr;

@property(nonatomic, retain)NSMutableArray *restaurantArr;

@property(nonatomic, retain)NSMutableArray *amuseArr;

@property(nonatomic, retain)NSMutableArray *shopingArr;

@property(nonatomic, retain)UICollectionView *collectionView;

@property(nonatomic, retain)NSMutableArray *array;


//@property(nonatomic, retain)UIScrollView *scrollView;

@end

@implementation NeighbouringViewController

- (void)dealloc{
    
//    [_scrollView release];
    [_array release];
    [_allArr release];
    [_scenicArr release];
    [_stayArr release];
    [_restaurantArr release];
    [_amuseArr release];
    [_shopingArr release];
    [_tableView release];
    [_collectionView release];
    [_tableView release];
    [_titleArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.allArr = [NSMutableArray array];
        self.scenicArr = [NSMutableArray array];
        self.stayArr = [NSMutableArray array];
        self.restaurantArr = [NSMutableArray array];
        self.amuseArr = [NSMutableArray array];
        self.shopingArr = [NSMutableArray array];
        self.titleArr = @[@"全部",@"景点",@"住宿",@"餐厅",@"休闲娱乐",@"购物"];
        self.array = [NSMutableArray arrayWithObjects:@"0",@"0",@"0",@"0",@"0",@"0", nil];
        self.isSelected = 0;
    }
    return self;
}


- (void)createAllData{
    
    //初始化数组
    //
    
//    NSInteger intTemp = [self.array[0] intValue];
    
    NSString *allUrl = [NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?keyword=&category=0&start=%@&count=20&latitude=38.883793&longitude=121.544871&sign=75bb1aa7c7e340f81e494b16d7c0f37f/",self.array[0]];
    if ([self.array[0] isEqualToString:@"0"]) {
        self.allArr = [NSMutableArray array];
    }
    
    [HTTPTool get:allUrl body:nil httpResult:JSON success:^(id result) {
        
        NSArray *arr = result[@"items"];
        
        for (NSDictionary *dic in arr) {
            NeighbouringModel *temp = [[NeighbouringModel alloc]initWithDic:dic];
            
            [self.allArr addObject:temp];
            [temp release];
        }


        NSIndexPath *temp = [NSIndexPath indexPathForRow:self.collectionView.contentOffset.x/WIDTH inSection:0];
        
        [self.collectionView cellForItemAtIndexPath:temp];
        
        NeighbouringDetailCell *test = (NeighbouringDetailCell *)[self.collectionView cellForItemAtIndexPath:temp];
        if ([self.array[0] isEqualToString:@"0"] ) {
            
            [test.tableView headerEndRefreshing];
        }else{
            [test.tableView footerEndRefreshing];
        }
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@" 全部  数据错误");
        
    }];
}

- (void)createScenicData{
    
    
    
    NSString *scenicUrl = [NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?keyword=&category=11&start=%@&count=20&latitude=38.883793&longitude=121.544871&sign=75bb1aa7c7e340f81e494b16d7c0f37f/",self.array[1]];
    
    if ([self.array[1] isEqualToString:@"0"]) {
        self.allArr = [NSMutableArray array];
    }
    
    [HTTPTool get:scenicUrl body:nil httpResult:JSON success:^(id result) {
        
        NSArray *arr = result[@"items"];
        
        for (NSDictionary *dic in arr) {
            NeighbouringModel *temp = [[NeighbouringModel alloc]initWithDic:dic];
            
            [self.scenicArr addObject:temp];
            [temp release];
        }
        
        
        NSIndexPath *temp = [NSIndexPath indexPathForRow:self.collectionView.contentOffset.x/WIDTH inSection:0];
        
        [self.collectionView cellForItemAtIndexPath:temp];
        
        NeighbouringDetailCell *test = (NeighbouringDetailCell *)[self.collectionView cellForItemAtIndexPath:temp];
        [test.tableView headerEndRefreshing];
        
        if ([self.array[1] isEqualToString:@"0"] ) {
            
            [test.tableView headerEndRefreshing];
        }else{
            [test.tableView footerEndRefreshing];
        }
        
        
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"景点数据解析错误");
    }];
}

- (void)createStayData{

    
    
    NSString *stayUrl =[NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?keyword=&category=10&start=%@&count=20&latitude=38.883793&longitude=121.544871&sign=75bb1aa7c7e340f81e494b16d7c0f37f/",self.array[2]];
    
    if ([self.array[2] isEqualToString:@"0"]) {
        self.allArr = [NSMutableArray array];
    }
    
    [HTTPTool get:stayUrl body:nil httpResult:JSON success:^(id result) {
        
        NSArray *arr = result[@"items"];
        
        for (NSDictionary *dic in arr) {
            NeighbouringModel *temp = [[NeighbouringModel alloc]initWithDic:dic];
            
            [self.stayArr addObject:temp];
            [temp release];
        }
        
        
        NSIndexPath *temp = [NSIndexPath indexPathForRow:self.collectionView.contentOffset.x/WIDTH inSection:0];
        
        [self.collectionView cellForItemAtIndexPath:temp];
        
        NeighbouringDetailCell *test = (NeighbouringDetailCell *)[self.collectionView cellForItemAtIndexPath:temp];
        [test.tableView headerEndRefreshing];
        
        if ([self.array[2] isEqualToString:@"0"] ) {
            
            [test.tableView headerEndRefreshing];
        }else{
            [test.tableView footerEndRefreshing];
        }
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"景点数据解析错误");
    }];
}

- (void)createRestaurantData{
    
    
    
    NSString *restaurantUrl =[NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?keyword=&category=5&start=%@&count=20&latitude=38.883793&longitude=121.544871&sign=75bb1aa7c7e340f81e494b16d7c0f37f/",self.array[3]];
    
    if ([self.array[3] isEqualToString:@"0"]) {
        self.allArr = [NSMutableArray array];
    }
    
    [HTTPTool get:restaurantUrl body:nil httpResult:JSON success:^(id result) {
        
        NSArray *arr = result[@"items"];
        
        for (NSDictionary *dic in arr) {
            NeighbouringModel *temp = [[NeighbouringModel alloc]initWithDic:dic];
            
            [self.restaurantArr addObject:temp];
            [temp release];
        }
        
        
        NSIndexPath *temp = [NSIndexPath indexPathForRow:self.collectionView.contentOffset.x/WIDTH inSection:0];
        
        [self.collectionView cellForItemAtIndexPath:temp];
        
        NeighbouringDetailCell *test = (NeighbouringDetailCell *)[self.collectionView cellForItemAtIndexPath:temp];
        [test.tableView headerEndRefreshing];
        
        if ([self.array[3] isEqualToString:@"0"] ) {
            
            [test.tableView headerEndRefreshing];
        }else{
            [test.tableView footerEndRefreshing];
        }
        
        
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"景点数据解析错误");
    }];
}

- (void)createAmuseData{
    
    
    
    NSString *amuseUrl =[NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?keyword=&category=21&start=%@&count=20&latitude=38.883793&longitude=121.544871&sign=75bb1aa7c7e340f81e494b16d7c0f37f/",self.array[4]];
    [HTTPTool get:amuseUrl body:nil httpResult:JSON success:^(id result) {
        
        if ([self.array[4] isEqualToString:@"0"]) {
            self.allArr = [NSMutableArray array];
        }
        
        NSArray *arr = result[@"items"];
        
        for (NSDictionary *dic in arr) {
            NeighbouringModel *temp = [[NeighbouringModel alloc]initWithDic:dic];
            
            [self.amuseArr addObject:temp];
            [temp release];
        }
        
        
        NSIndexPath *temp = [NSIndexPath indexPathForRow:self.collectionView.contentOffset.x/WIDTH inSection:0];
        
        [self.collectionView cellForItemAtIndexPath:temp];
        
        NeighbouringDetailCell *test = (NeighbouringDetailCell *)[self.collectionView cellForItemAtIndexPath:temp];
        [test.tableView headerEndRefreshing];
        
        if ([self.array[4] isEqualToString:@"0"] ) {
            
            [test.tableView headerEndRefreshing];
        }else{
            [test.tableView footerEndRefreshing];
        }
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"景点数据解析错误");
    }];
}


- (void)createShopingData{

    
    
    
    NSString *shopingUrl =[NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?keyword=&category=6&start=%@&count=20&latitude=38.883793&longitude=121.544871&sign=75bb1aa7c7e340f81e494b16d7c0f37f/",self.array[5]];
    [HTTPTool get:shopingUrl body:nil httpResult:JSON success:^(id result) {
        
        if ([self.array[5] isEqualToString:@"0"]) {
            self.allArr = [NSMutableArray array];
        }
        
        NSArray *arr = result[@"items"];
        
        for (NSDictionary *dic in arr) {
            NeighbouringModel *temp = [[NeighbouringModel alloc]initWithDic:dic];
            
            [self.shopingArr addObject:temp];
            [temp release];
        }
        
        
        NSIndexPath *temp = [NSIndexPath indexPathForRow:self.collectionView.contentOffset.x/WIDTH inSection:0];
        
        [self.collectionView cellForItemAtIndexPath:temp];
        
        NeighbouringDetailCell *test = (NeighbouringDetailCell *)[self.collectionView cellForItemAtIndexPath:temp];
        [test.tableView headerEndRefreshing];
        
        if ([self.array[5] isEqualToString:@"0"] ) {
            
            [test.tableView headerEndRefreshing];
        }else{
            [test.tableView footerEndRefreshing];
        }
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"景点数据解析错误");
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //数组初始化
//    self.titleArr = @[@"全部",@"景点",@"住宿",@"餐厅",@"休闲娱乐",@"购物"];
//    self.array = [NSArray arrayWithObjects:0,0,0,0,0,0, nil];
//    self.isSelected = 0;
    
    self.title = @"我的附近";

    
    [self createAllData];
    [self createScenicData];
    [self createStayData];
    [self createRestaurantData];
    [self createAmuseData];
    [self createShopingData];
    [self createTableView];
    [self createCollectionView];
}



/**
 *  创建collecView
 */
- (void)createCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置每个item的大小
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT-30-64);
    
    //设置最小的行列间距
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    //设置滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //设置四周边框
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30, WIDTH, HEIGHT-30-64) collectionViewLayout:flowLayout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.pagingEnabled = YES;
    [self.view addSubview:self.collectionView];
    
    //注册
    [self.collectionView registerClass:[NeighbouringDetailCell class] forCellWithReuseIdentifier:@"detailReuse"];
    
    
    
    [self.collectionView release];
    [flowLayout release];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NeighbouringDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"detailReuse" forIndexPath:indexPath];
    
    [cell.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    
     [cell.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    if (self.allArr.count != 0) {
        
        cell.allArr = self.allArr;
    }
    
    if (self.scenicArr.count != 0) {
        cell.scenicArr = self.scenicArr;
    }
    
    if (self.stayArr.count != 0) {
        cell.stayArr = self.stayArr;
    }
    
    if (self.restaurantArr.count != 0) {
        cell.restaurantArr = self.restaurantArr;
    }
    
    if (self.amuseArr.count != 0) {
        cell.amuseArr = self.amuseArr;
    }
    
    if (self.shopingArr.count != 0) {
        cell.shopingArr = self.shopingArr;
    }
    
    cell.temp = self.titleArr[indexPath.row];
    return  cell;
}

- (void)headerRereshing{
    
    NSInteger temp = self.collectionView.contentOffset.x/WIDTH ;
    
    self.array[temp] = @"0";
    
    if (temp == 0) {
        [self createAllData];
    }else if (temp == 1){
        [self createScenicData];
    }else if (temp == 2){
        [self createStayData];
    }else if (temp == 3){
        [self createRestaurantData];
    }else if (temp == 4){
        [self createAmuseData];
    }else{
        [self createShopingData];
    }
    
    
}


- (void)footerRereshing {
    
    NSInteger temp = self.collectionView.contentOffset.x/WIDTH ;
    
    NSInteger intTemp = [self.array[temp] intValue];
    NSLog(@"%ld asdfsadf",intTemp);
    self.array[temp] = [NSString stringWithFormat:@"%ld", (intTemp + 20)];
    NSLog(@"%@",self.array[temp]);
    
    
    if (temp == 0) {
        [self createAllData];
    }else if (temp == 1){
        [self createScenicData];
    }else if (temp == 2){
        [self createStayData];
    }else if (temp == 3){
        [self createRestaurantData];
    }else if (temp == 4){
        [self createAmuseData];
    }else{
        [self createShopingData];
    }
    
}


/**
 *  创建title标题
 */
- (void)createTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(WIDTH/2-15, -0.26*HEIGHT, 30, WIDTH) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor cyanColor];
    //tableview逆时针旋转90度。
    self.tableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
//    self.tableView.rowHeight = 100;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.titleArr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
        static NSString *reuse = @"titleReuse";
        NeighbouringCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[NeighbouringCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse]autorelease];
        }
        cell.titleLabel.text = self.titleArr[indexPath.row];
        [cell.titleLabel sizeToFit];
        cell.transform = CGAffineTransformMakeRotation(M_PI / 2);
        //点击cell不变色
        //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //改变选中cell时的颜色
        cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.titleLabel.frame.size.width+20, 25)] autorelease];
        if (indexPath.row == 4) {
            
            cell.selectedBackgroundView.center = CGPointMake(60, 14);
        }else{
            cell.selectedBackgroundView.center = CGPointMake(40, 14);
        }
    
    
        cell.selectedBackgroundView.layer.cornerRadius = 12;
        
        cell.selectedBackgroundView.backgroundColor = [UIColor cyanColor];
        
//        self.test = self.titleArr[0];
    
        return  cell;
    
}

//




//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row == 4) {
        return 120;
    }else{
        
        return 80;
    }
    
}


//
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    [self.collectionView setContentOffset:CGPointMake(WIDTH * indexPath.row, 0) animated:YES];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == self.collectionView) {
        //设定选中
        NSIndexPath *temp = [NSIndexPath indexPathForRow:self.collectionView.contentOffset.x/WIDTH inSection:0];
        [self.tableView selectRowAtIndexPath:temp animated:YES scrollPosition:UITableViewScrollPositionMiddle];
    }
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSIndexPath *first = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView selectRowAtIndexPath:first animated:YES scrollPosition:UITableViewScrollPositionTop];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
